  function plots_mnc(mat,in); 
% function plots_mnc(mat,in);
%
% PLOTS FOR T X C X P MATRICES 
% =====================================
% Plot all C columns of each of P pages in 
% panels of M x N plots each 
% Can use with a T x C matrix in which case m = n = 1 
% 
% Inputs 
% =====
% mat         matrix
% in          structure  
%
% Structure in allows for all other options, default values in parenthesis 
% =========================================================================
%
% in.m          Plots will be divided into panels of m x n plots per page 
%               this defines number of rows ( 2 ) 
%
% in.n          Plots will be divided into panels of m x n plots per page 
%               this defines number of colums ( 2 ) 
%
% in.gname   File for the graphs, which will be stored as 
%               in.gname_<number>, ( [] in which case plots not saved )  
%   
% in.opath      Output folder where graphs will be stored (  current cd ) 
%
% in.cnames     columns names, cell with dimension C 
%               ( [col1 col2 ....colC] )
%
% in.pnames     pages names, cell with dimension P 
%               ( [page1,...,pageP] ) 
%
% in.title      string with middle of title. Title will be 
%               [cname  in.title pname ], ( fig ) 
% 
% in.legend     cell of legends, of dimension C x 1 
%               ( col1, col2 ,..... ) 
% 
% in.axfont      Fonts size for axes ( 7 ) 
% 
% in.titfont     Font size for titles ( 8 )  

% in.xaxis      X-axis, [T x 1] ( [1:T] ) 
% 
% in.ylim       Limits for the y-axis 
%               Two possibilities. 
%               1) If dimension 2x1 then (1,1) Lower Bound ; (2,1) Upper Bound
%               2) dimension [2 2],  [   substract from min   Lower Bound; 
%                                         add       from max   Upper Bound ]
%               ( [], axis tight ) 
%
% in.ygrid      == 1  Y grid on  ( 0 , Off ) 
%
% in.xgrid      == 1  X grid on  ( 0 , Off )
%
%
% Alejandro Justiniano 5/10/2005 
%
% 
%==========================================================================
%==========================================================================

cucd=cd; 

if nargin == 1  
    in.m = 2 ; 
else 
    if isstruct( in ) == 0 
        error('Input in must be a structure') 
    end 
end 


dim=size(mat); 
if length( dim ) == 2 
    in.m = 1; 
    in.n = 1; 
    np = 1; 
elseif length( dim ) == 3 
    np = dim(end); 
else 
    error('Input mat must be 2 or 3 dimensional') 
end 
T=dim(1); 
nc=dim(2); 

% ================================================
% Check fields and assign default values otherwise
% ================================================
if np > 1 
    [e,in]=ch_field( in ,'m', 2 );
    if np < 3 
        [e,in]=ch_field( in ,'n' ,1 );
    else 
        [e,in]=ch_field( in ,'n' ,2 );
    end 
end 

[e,in]=ch_field( in, 'gname' ); 
if e == 0 
    flag_save = 0 ; 
else 
    flag_save = 1; 
    [e,in] = ch_field( in, 'opath', cucd ) ; 
end 

% Column names 
% -------------
[e,in]=ch_field( in, 'cnames' ); 
if e==0 
    in.cnames = fnumcell('col ',[1:nc] ); 
end 
if length( in.cnames ) ~= nc ; 
    error('Length of in.cnames does not match number of columns in mat') 
end 

% Page names 
% -------------
[e,in]=ch_field( in, 'pnames' ); 
if e==0 
    in.pnames = fnumcell('page ',[1:np] ); 
else 
    if length( in.pnames ) ~= np ; 
        error('Length of in.pnames does not match number of pages in mat') 
    end 
end 

[e,in]=ch_field( in, 'title', ' ' );
[e,in]=ch_field( in, 'legend' , in.cnames );
[e,in]=ch_field( in, 'axfont',  7 );
[e,in]=ch_field( in, 'xaxis',  [1:T]' );

if e ~= 0 
    in.xaxis=in.xaxis(:); 
    temp=length(in.xaxis(:) ); 
    if temp~=T 
        error('Length of in.xaxis does not match number of rows in mat') 
    end 
    clear temp; 
end 

[e,in]=ch_field( in, 'titfont', 8 );
[e,in]=ch_field( in, 'ygrid', 0 );
[e,in]=ch_field( in, 'xgrid', 0 );

% ===========================
% Vector of limist for y-axis 
% ===========================

[e,in]=ch_field( in, 'ylim' );
if e==0 
    flag_ylim = 0 ; 
else 
    temp = size( in.ylim ); 
    if isequal( sort(temp) ,[1 2] )== 1 
        flag_ylim = 1 ; 
    else 
        if isequal( temp , [2 2] ) == 1 
            flag_ylim = 2; 
        else 
            error('in.ylim must be of dimension [2 1] or [2 2]' ) 
        end 
    end 
end 
    
% =========================================================    

set(0,'DefaultAxesFontSize',in.axfont);
if in.ygrid ~= 0 
set(0,'DefaultAxesYGrid','on'); 
end 
if in.xgrid ~= 0 
set(0,'DefaultAxesXGrid','on'); 
end 

% Plot Line Style indicators 
% ==========================
%style={ 'k-';'m-'; 'k:' ;'m:' ; '.-k' ; '.-c' }
style={ 'b-','r:','k.-','b+:','r*:','kx:' }; 
width =[ 1.3  ; 1.5 ; 1.2 ; 1.3 ; 1.5 ; 1.2 ] ; 

ppg = in.m*in.n; 
ipg = 0; 
countg = 0; 

% ============================
%         Begin Plots 
% ============================
hh=1; 
for hh=1:np; 
   
    if ipg == 0 
        figure; 
    end 
    
    ipg = ipg + 1; 
    subplot(in.m,in.n, ipg ); 
    
    hold on; 
    ii=1; 
    for ii=1:nc 
        plot( in.xaxis , mat(:,ii,hh) , style{ii} ,'LineWidth', width(ii)  );
    end 
    hold off; 
    
    title( [in.pnames{hh}, in.title ] ,'FontSize', in.titfont ) ;  
    
    if ipg == 2 | np == 1 
       lh =  legend( in.legend , 0 ) ; 
       set(lh,'FontSize',in.axfont );
       legend boxoff 
   end 
   
   switch flag_ylim      
       case 1 
           YLim( [in.ylim(1) in.ylim(2)] ); 
       case 2 
           temp_lb = min( min( mat(:,:,hh) ) ); 
           temp_lb = max( [temp_lb - in.ylim(1,1) ; in.ylim(1,2) ] ); 
           temp_ub = max( max( mat(:,:,hh) ) ); 
           temp_ub = min( [temp_ub + in.ylim(2,1) ; in.ylim(2,2) ] ); 
           YLim( [temp_lb temp_ub] ); 
   end 
   
   if ipg == ppg | hh == np     
       ipg = 0; 
       countg = countg + 1; 
       
       if flag_save == 1; 
           cd( in.opath ); 
           saveas(gcf, [in.gname,'_',num2str( countg ) ] ,'fig' ); 
           cd(cucd ) 
       end 
   end 
  
end 

set(0,'DefaultAxesFontSize','remove');
if in.ygrid ~= 0 
    set(0,'DefaultAxesYGrid','remove'); 
end 
if in.xgrid ~= 0 
    set(0,'DefaultAxesXGrid','remove'); 
end 

